//
//  CommonScene.cpp
//  p1
//
//  Created by yoona park on 13. 9. 30..
//
//
#include "cocos-ext.h"
#include <cstdio>
#include <string>

#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "AppDelegate.h"
#include "CommonScene.h"

using namespace cocos2d;
using namespace CocosDenshion;

bool Common::init()
{
    //////////////////////////////
    // 1. super init first
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    //setDefine();
    //loadingEnd();
    return true;
}

void Common::loadingStart(std::string msg)
{
    CCLog("loadingStart: %s", msg.c_str());
    this->setTouchEnabled(false);
}
void Common::loadingEnd()
{
    this->setTouchEnabled(true);
}

void Common::setDefine()
{
}

void Common::delayCCCallFunc(float delay, cocos2d::SEL_CallFunc selector)
{
    CCDelayTime *delayAction = CCDelayTime::create(delay);
    CCCallFunc *callSelectorAction = CCCallFunc::create(this, selector);
    this->runAction(CCSequence::create(delayAction, callSelectorAction, NULL));
}

void Common::delayCCCallFuncN(float delay, cocos2d::SEL_CallFuncN selector, CCObject* pObject)
{
    CCDelayTime *delayAction = CCDelayTime::create(delay);
    CCCallFuncN *callSelectorAction = CCCallFuncN::create(pObject, selector);
    ((CCNode*)pObject)->runAction(CCSequence::create(delayAction, callSelectorAction, NULL));
}


bool Common::backgroundTimeCheck()
{
    int nbTime = CCUserDefault::sharedUserDefault()->getIntegerForKey("background_time",0);
    int nNowTime = time(NULL);
    if (nbTime > 0 && (nNowTime - nbTime)>BACKGROUND_RESTART_SEC)
    {
        unscheduleAllSelectors();
        stopAllActions();
        CCLog("nNowTime - nbTime %d", (nNowTime - nbTime));
        return true;
    }
    CCUserDefault::sharedUserDefault()->setIntegerForKey("background_time",0);
    return false;
}

void Common::exitApp()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    //java_funcType1("finishApp");
#else
    exit(0);
#endif
    
}
